package com.version.manager.service;

import com.version.manager.annotaion.CacheExpire;
import com.version.manager.constant.AdminCommonConstant;
import com.version.manager.entity.Menu;
import com.version.manager.entity.Role;
import com.version.manager.mapper.MenuMapper;
import com.version.manager.model.Msg;
import com.version.manager.model.TreeNode;
import com.version.manager.util.RedisUtil;
import com.version.manager.util.TreeUtil;
import com.version.manager.vo.MenuTree;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class MenuService extends BaseService<MenuMapper,Menu>{

    @Resource
    private MenuMapper menuMapper;
    @Cacheable(value = "menu",key = "'role_menu_'.concat(#root.args[0])",sync=true)
    @CacheExpire(expire = 60)
    public List<Menu> getMenuByRole(String roleId){
        List<Menu> menu=menuMapper.getMenuByRole(roleId);
        return menu;
    }
    private void getRoleMenuTree(List<TreeNode> menuTrees, List<Menu> menus, List<Menu> result){
        for(TreeNode t:menuTrees){
            if(t.getChildren()==null||t.getChildren().size()==0){
                for(Menu m:menus){
                    if(m.getId()==t.getId()){
                        result.add(m);
                    }
                }
            }
            else{
                getRoleMenuTree(t.getChildren(),menus,result);
            }
        }
    }
    private List<MenuTree> getMenuTree(List<Menu> menus, int root) {
        List<MenuTree> trees = new ArrayList<MenuTree>();
        MenuTree node = null;
        for (Menu menu : menus) {
            node = new MenuTree();
            BeanUtils.copyProperties(menu, node);
            trees.add(node);
        }
        return TreeUtil.build(trees, root);
    }

    @CacheEvict(value = "MenuTree",allEntries =true )
    public Msg add(Menu menu) throws Exception {
        Msg msg= super.saveOrUpdate(menu);
        return msg;
    }
    @Cacheable(value = "MenuTree",key = "'MenuTree1'",sync=true)
    @CacheExpire(expire = 60)
    public List<MenuTree> getMenuTree(){
        List<Menu> menus=menuMapper.selectAll();
        List<MenuTree> menuTrees= getMenuTree(menus, AdminCommonConstant.ROOT);
        return menuTrees;

    }
    public List<Menu> selectAll(){
        List<Menu> menus= menuMapper.selectAll();
        return menus;
    }

    public int deleteRoleMenu(Role role){
        return  menuMapper.deleteRoleMenu((role.getId().toString()));
    }
    public int addRoleMenu(Role role,List<Integer> menuIds){
        return menuMapper.addRoleMenu(role.getId().toString(),menuIds);
    }
}
